using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._0930;

public class User
{
    private List<UserInfo> userList = new List<UserInfo>();

    // 显示主菜单
    public void Show()
    {
        while (true)
        {
            Console.WriteLine("主菜单：");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请输入选项（1-3）：");

            string choice = Console.ReadLine();

            if (choice == "1")
            {
                Login();
            }
            else if (choice == "2")
            {
                Regist();
            }
            else if (choice == "3")
            {
                Console.WriteLine("退出程序。");
                break;
            }
            else
            {
                Console.WriteLine("输入错误，请重新输入。");
            }
        }
    }

    // 用户登录
    public void Login()
    {
        Console.WriteLine("===== 登录 =====");
        Console.Write("用户名：");
        string username = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不可为空。");
            return;
        }

        Console.Write("密码：");
        string password = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("密码不可为空。");
            return;
        }

        UserInfo user = userList.Find(u => u.Username == username);
        if (user == null)
        {
            Console.WriteLine("用户名不存在。");
        }
        else if (user.Password != password)
        {
            Console.WriteLine("密码错误。");
        }
        else
        {
            Console.WriteLine("登录成功，进入系统首页（模拟）。");
        }
    }

    // 用户注册
    public void Regist()
    {
        Console.WriteLine("===== 注册 =====");
        Console.Write("用户名：");
        string username = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(username))
        {
            Console.WriteLine("用户名不可为空。");
            return;
        }

        if (username.Length < 3 || username.Length > 15)
        {
            Console.WriteLine("用户名长度必须为 3-15 个字符。");
            return;
        }

        if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
        {
            Console.WriteLine("用户名只能包含字母、数字、下划线。");
            return;
        }

        if (userList.Exists(u => u.Username == username))
        {
            Console.WriteLine("用户名已存在。");
            return;
        }

        Console.Write("密码：");
        string password = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(password))
        {
            Console.WriteLine("密码不可为空。");
            return;
        }

        if (password.Length < 6 || password.Length > 20)
        {
            Console.WriteLine("密码长度必须为 6-20 个字符。");
            return;
        }

        if (!Regex.IsMatch(password, @"[A-Z]") || !Regex.IsMatch(password, @"[a-z]") || !Regex.IsMatch(password, @"[0-9]"))
        {
            Console.WriteLine("密码必须包含至少 1 个大写字母、1 个小写字母和 1 个数字。");
            return;
        }

        Console.Write("确认密码：");
        string confirmPassword = Console.ReadLine();

        if (string.IsNullOrWhiteSpace(confirmPassword))
        {
            Console.WriteLine("确认密码不可为空。");
            return;
        }

        if (password != confirmPassword)
        {
            Console.WriteLine("密码与确认密码不一致。");
            return;
        }

        userList.Add(new UserInfo { Username = username, Password = password });
        Console.WriteLine("注册成功，自动返回主菜单。");
    }
}

public class UserInfo
{
    public string Username { get; set; }
    public string Password { get; set; }
}